package com.mashiro.system.service.impl;

import com.mashiro.common.utils.DateUtils;
import com.mashiro.system.domain.MoeFund;
import com.mashiro.system.domain.MoeMaterial;
import com.mashiro.system.domain.MoeTask;
import com.mashiro.system.domain.status.MoeTaskStatus;
import com.mashiro.system.mapper.MoeFundMapper;
import com.mashiro.system.mapper.MoeMaterialMapper;
import com.mashiro.system.mapper.MoeTaskMapper;
import com.mashiro.system.service.IMoeTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 任务Service业务层处理
 *
 * @author mashiro
 * @date 2023-04-18
 */
@Service
public class MoeTaskServiceImpl implements IMoeTaskService {
    @Resource
    private MoeTaskMapper moeTaskMapper;
    @Resource
    private MoeFundMapper moeFundMapper;
    @Resource
    private MoeMaterialMapper moeMaterialMapper;

    /**
     * 查询任务
     *
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public MoeTask selectMoeTaskById(Long id) {
        return moeTaskMapper.selectMoeTaskById(id);
    }

    /**
     * 查询任务列表
     *
     * @param moeTask 任务
     * @return 任务
     */
    @Override
    public List<MoeTask> selectMoeTaskList(MoeTask moeTask) {
        return moeTaskMapper.selectMoeTaskList(moeTask);
    }

    /**
     * 新增任务
     *
     * @param moeTask 任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMoeTask(MoeTask moeTask) {
        Date nowDate = DateUtils.getNowDate();
        moeTask.setCreateTime(nowDate);
        if (moeTask.getTaskParentId() == 0) {
            if (moeTask.getWorkload() == 100L) {
                moeTask.setStatus(MoeTaskStatus.COMPLETED.getValue());
                moeTask.setFinishTime(nowDate);
            } else {
                if (MoeTaskStatus.COMPLETED.getValue().equals(moeTask.getStatus())) {
                    moeTask.setStatus(MoeTaskStatus.IN_PROGRESS.getValue());
                    moeTask.setFinishTime(null);
                }
            }
            moeTaskMapper.insertMoeTask(moeTask);
            MoeFund moeFund = new MoeFund();
            moeFund.setProjectId(moeTask.getProjectId());
            moeFund.setTaskId(moeTask.getId());
            moeFund.setName(moeTask.getName());
            moeFund.setCreateTime(nowDate);
            return moeFundMapper.insertMoeFund(moeFund);
        }
        return moeTaskMapper.insertMoeTask(moeTask);
    }

    /**
     * 修改任务
     *
     * @param moeTask 任务
     * @return 结果
     */
    @Override
    public int updateMoeTask(MoeTask moeTask) {
        Date nowDate = DateUtils.getNowDate();
        moeTask.setUpdateTime(nowDate);
        if (moeTask.getTaskParentId() == 0) {
            if (moeTask.getWorkload() == 100L) {
                moeTask.setStatus(MoeTaskStatus.COMPLETED.getValue());
                moeTask.setFinishTime(nowDate);
            } else {
                if (MoeTaskStatus.COMPLETED.getValue().equals(moeTask.getStatus())) {
                    moeTask.setStatus(MoeTaskStatus.IN_PROGRESS.getValue());
                    moeTask.setFinishTime(null);
                }
            }
        }
        return moeTaskMapper.updateMoeTask(moeTask);
    }

    /**
     * 批量删除任务
     *
     * @param ids 需要删除的任务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMoeTaskByIds(Long[] ids) {
        int i = 0;
        for (Long id : ids) {
            i += this.deleteMoeTaskById(id);
        }
        return i;
    }

    /**
     * 删除任务信息
     *
     * @param id 任务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMoeTaskById(Long id) {
        MoeFund moeFund = new MoeFund();
        moeFund.setTaskId(id);
        List<MoeFund> moeFunds = moeFundMapper.selectMoeFundList(moeFund);
        Long[] moeFundIds = moeFunds.stream().map(MoeFund::getId).toArray(Long[]::new);
        if (moeFundIds.length > 0) {
            moeFundMapper.deleteMoeFundByIds(moeFundIds);
        }
        MoeMaterial moeMaterial = new MoeMaterial();
        moeMaterial.setTaskId(id);
        List<MoeMaterial> moeMaterials = moeMaterialMapper.selectMoeMaterialList(moeMaterial);
        Long[] moeMaterialIds = moeMaterials.stream().map(MoeMaterial::getId).toArray(Long[]::new);
        if (moeMaterialIds.length > 0) {
            moeMaterialMapper.deleteMoeMaterialByIds(moeMaterialIds);
        }
        return moeTaskMapper.deleteMoeTaskById(id);
    }

    /**
     * 是否存在任务子节点
     *
     * @param id 任务ID
     * @return 结果
     */
    @Override
    public boolean hasChildByTaskId(Long id) {
        int result = moeTaskMapper.hasChildByTaskId(id);
        return result > 0;
    }
}
